package cn.zmwh.im.server.modules.message.service;

import cn.hutool.core.util.StrUtil;
import cn.zmwh.im.common.pojo.dto.IMUserInfo;
import cn.zmwh.im.server.api.Result;
import cn.zmwh.im.server.constant.IMRedisKey;
import cn.zmwh.im.server.modules.message.pojo.request.HistoryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 群消息 服务实现类
 * </p>
 *
 * @author dmzmwh
 * @since 2025-06-19
 */
@Service
public class MessageLogService {

    @Autowired
    private ImMessageService imMessageService;
    @Autowired
    private ImGroupMessageService imGroupMessageService;
    @Autowired
    private RedisTemplate redisTemplate;


    public Result history(HistoryRequest request) {
        Long uid = request.getUid();
        String terminal = request.getTerminal();
        Object serverKey = redisTemplate.opsForValue().get(StrUtil.join(":", IMRedisKey.GAIN_SERVER_ID, uid, terminal));
        if (serverKey == null) {
            return Result.failed("没有建立链接！");
        }
        String serverId = serverKey.toString();
        IMUserInfo receiver = new IMUserInfo(uid, terminal);
        receiver.setServerId(serverId);
        int type = request.getType();
        if (type == 0) {
            imMessageService.history(request, receiver);
        } else if (type == 1) {
            imGroupMessageService.history(request, receiver);
        }
        return Result.success();
    }
}
